#include "UdpHandleTask.h"

bool Call::UdpHandleTask::Create()
{
    // Create Udp Server
    WS::UdpServer::Desc desc;

    Call::Config config;
    desc.port = config.udpPort;

    SV_FAILED(udpServer.Create(desc));
    return true;
}

void Call::UdpHandleTask::Run()
{
    while (1)
    {
        WS::UdpSocket from;
        CharBuf buf;

        int len = udpServer.RecvFrom<CharBuf>(buf, from);

        if (len > 0)
        {
            OnUdpRead(buf, from);
        }
        else
        {
            OnUdpReadError(from);
        }
    }
}

void Call::UdpHandleTask::OnUdpRead(const CharBuf &buf, const WS::SockAddrIn &from)
{
    LOGI("recv from socket %s %d", from.IP(), from.Port());
    LOGI("buf is %d", buf.no);

    CharBuf rep;
    rep.no = buf.no + 100;

    int len = udpServer.SendTo<CharBuf>(rep, from);
    LOGI("rep send %d", len);
}

void Call::UdpHandleTask::OnUdpReadError(const WS::SockAddrIn &from)
{
    LOGI("udp Read error");
}
